package com.itfreer.ui.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.itfreer.ui.R;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 字典多选Adapter
 */
public class DomainCheckAdapter extends BaseAdapter {

    private Context context;
    private Map<Object, Object> domain;
    private String[] keySets;
    private List<Object> valueList;
    private LinkedHashMap<Object, CheckBox> checkBoxMap = new LinkedHashMap<Object, CheckBox>();

    public DomainCheckAdapter(Context context, Map<Object, Object> domain){
        this.context = context;
        this.domain = domain;

        if(domain!=null && domain.size()>0){
            keySets = new String[domain.size()];
            keySets = domain.keySet().toArray(keySets);
        }
    }

    @Override
    public int getCount() {
        if(keySets!=null){
            return keySets.length;
        }else{
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        if(keySets!=null){
            return keySets[position];
        }else{
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 设置当前选择项
     */
    public void setCheckedValue(List<Object> valueList){
        Set<Object> rKeys = checkBoxMap.keySet();
        for (Object rKey : rKeys) {
            if(valueList!=null && valueList.contains(rKey)){
                checkBoxMap.get(rKey).setChecked(true);
            } else {
                checkBoxMap.get(rKey).setChecked(false);
            }
        }
        this.valueList = valueList;
    }

    /**
     * 获取值
     * @return
     */
    public List<Object> getCheckedValue(){
        List<Object> values = new ArrayList<Object>();
        Set<Object> rKeys = checkBoxMap.keySet();
        for (Object rKey : rKeys) {
            if(checkBoxMap.get(rKey).isChecked()){
                values.add(rKey);
            }
        }
        valueList = values;
        return valueList;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.com_itfreer_core_ui_dialog_domaindialog_check, null);

            CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.com_itfreer_core_ui_dialog_domaindialog_check_checkbox);
            TextView textView = (TextView) convertView.findViewById(R.id.com_itfreer_core_ui_dialog_domaindialog_check_textview);

            String text = domain.get(keySets[position]).toString();
            textView.setText(text);
            checkBox.setTag(keySets[position]);

            if(checkBoxMap.containsKey(keySets[position])){
                checkBox.setChecked(checkBoxMap.get(keySets[position]).isChecked());
            }
            checkBoxMap.put(keySets[position], checkBox);
        }
        return convertView;
    }
}